package com.cuit.qx.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.qx.entity.Inspection;
import com.cuit.qx.entity.Product;
import com.cuit.qx.service.InspectionService;
import com.cuit.qx.utils.ReturnData;
import com.cuit.qx.utils.ReturnStateCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 数据检验
 * </p>
 *
 * @author lcj
 * @since 2021-11-25
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/inspection")
public class InspectionController {

    @Resource
    private InspectionService inspectionService;

    @PostMapping("/list")
    public ReturnData getList(@RequestBody Map<String, Object> map) {
        String startDate = (String) map.get("startDate");
        String startTime = (String) map.get("startTime");
        String stationName = (String) map.get("stationName");
        Integer pageNo = (Integer) map.get("pageNo");
        Integer pageSize = (Integer) map.get("pageSize");
        LambdaQueryWrapper<Inspection> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(startDate)) {
            queryWrapper.eq(Inspection::getStartDate, startDate);
        }
        if (StrUtil.isNotBlank(startTime)) {
            queryWrapper.eq(Inspection::getStartTime, startTime);
        }
        if (StrUtil.isNotBlank(stationName)) {
            queryWrapper.like(Inspection::getStaionName, stationName);
        }
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        Page<Inspection> page = new Page<>(pageNo, pageSize);
        IPage<Inspection> pageList = inspectionService.page(page, queryWrapper);
        if (pageList.getRecords().isEmpty()){
            return new ReturnData(ReturnStateCode.SUCCEEBUT);
        }
        return new ReturnData(ReturnStateCode.SUCCEE,page, (int) page.getTotal());
    }

}
